package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Api(tags = "菜品相关接口")
@RestController
@Slf4j
@RequestMapping("/admin")
public class DishController {
    /**
     * 新增菜品
     */
    @Autowired
    DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;

    @ApiOperation("新增菜品")
    @PostMapping("/dish")
    public Result addDish(@RequestBody DishDTO dishDTO){
        log.info("添加菜品");
        dishService.addDish(dishDTO);
        //精确清除缓存
        String key = "dish_"+dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }
    /**
     * 菜品分页查询
     */
    @ApiOperation("菜品分页")
    @GetMapping("/dish/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 批量删除菜品
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping("/dish")
    public Result delete(@RequestParam List<Long> ids){
        log.info("菜品批量删除");
        dishService.deleteBench(ids);
        //清除缓存
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 根据Id查找菜品
     */
    @ApiOperation("根据id查找菜品")
    @GetMapping("/dish/{id}")
    public Result<DishVO> getbyid(@PathVariable Long id){
        log.info("查询菜品");
        DishVO dishVO = dishService.getbyid(id);
        return Result.success(dishVO);
    }
    /**
     * 修改菜品及菜品口味
     */
    @ApiOperation("更新菜品及口味")
    @PutMapping("/dish")
    public Result update(@RequestBody DishDTO dishDTO){
        dishService.update(dishDTO);
        //清除缓存
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 根据分类Id查询菜品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类Id查询菜品")
    @GetMapping("dish/list")
    public Result<List<Dish>> getByCategoryId(Long categoryId){
        List<Dish> dishList = dishService.getByCategoryId(categoryId);
        return Result.success(dishList);
    }

    /**
     * 更改菜品售卖状态
     * @param status
     * @return
     */
    @ApiOperation("菜品启停售")
    @PostMapping("/dish/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("菜品状态为{}",status==1?"在售":"停止售卖");
        dishService.setStatus(status,id);
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 清理缓存
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
